package org.innovasix.educational.javap.domain.constantpool;

import org.innovasix.educational.javap.domain.datatypes.cp_info;
import org.innovasix.educational.javap.domain.datatypes.u1;
import org.innovasix.educational.javap.domain.parser.ByteQueue;
import org.innovasix.educational.javap.exception.InvalidArgumentException;

public class ConstantPoolEntryFactory {

	public static cp_info createNextEntry(ByteQueue byteQueue) {
		
		u1 tag = byteQueue.pollOneByte("ConstantPoolEntryFactory.createNextEntry");
		
		if(ConstantPoolTags.CONSTANT_Utf8.decimalValue == tag.getValue())
			return new CONSTANT_Utf8_info(byteQueue);
		
		if(ConstantPoolTags.CONSTANT_Integer.decimalValue == tag.getValue())
			return new CONSTANT_Integer_info(byteQueue);
		
		if(ConstantPoolTags.CONSTANT_Float.decimalValue == tag.getValue())
			return new CONSTANT_Float_info(byteQueue);

		if(ConstantPoolTags.CONSTANT_Long.decimalValue == tag.getValue())
			return new CONSTANT_Long_info(byteQueue);

		if(ConstantPoolTags.CONSTANT_Double.decimalValue == tag.getValue())
			return new CONSTANT_Double_info(byteQueue);
		
		if(ConstantPoolTags.CONSTANT_Class.decimalValue == tag.getValue())
			return new CONSTANT_Class_info(byteQueue);
		
		if(ConstantPoolTags.CONSTANT_String.decimalValue == tag.getValue())
			return new CONSTANT_String_info(byteQueue);
		
		if(ConstantPoolTags.CONSTANT_Fieldref.decimalValue == tag.getValue())
			return new CONSTANT_Fieldref_info(byteQueue);
		
		if(ConstantPoolTags.CONSTANT_Methodref.decimalValue == tag.getValue())
			return new CONSTANT_Methodref_info(byteQueue);
		
		if(ConstantPoolTags.CONSTANT_InterfaceMethodref.decimalValue == tag.getValue())
			return new CONSTANT_InterfaceMethodref_info(byteQueue);

		if(ConstantPoolTags.CONSTANT_NameAndType.decimalValue == tag.getValue())
			return new CONSTANT_NameAndType_info(byteQueue);
		
		if(ConstantPoolTags.CONSTANT_MethodHandle.decimalValue == tag.getValue())
			return new CONSTANT_MethodHandle_info(byteQueue);
		
		if(ConstantPoolTags.CONSTANT_MethodType.decimalValue == tag.getValue())
			return new CONSTANT_MethodType_info(byteQueue);
		
		if(ConstantPoolTags.CONSTANT_InvokeDynamic.decimalValue == tag.getValue())
			return new CONSTANT_InvokeDynamic_info(byteQueue);

		throw new InvalidArgumentException(tag + " is an undefined constant pool tag");
	}
	
}
